package lxq.asia;

import java.util.Arrays;

/**
 * 数组实现队列（还未实现环形队列，只能用一次）
 */
public class ArrayQueue {

    private final int maxSize;

    /**
     * 指向最后一个元素
     */
    private int rear;
    /**
     * 指向第一个队头的前一个元素
     */
    private int front;
    private final int[] queue;

    public ArrayQueue(int max){
        maxSize=max;
        queue=new int[maxSize];
        rear=-1;
        front=-1;
    }
    public ArrayQueue(){
        this(10);
    }

    public int pop(){
        if (isEmpty()) {
            throw new  RuntimeException("队列为空");
        }
        front+=1;
        return queue[front];
    }
    public void push(int num){
        if (isFull()){
//            throw new  RuntimeException("队列已满");
            System.out.println("队列已满");
            return;
        }
        rear+=1;
        queue[rear]=num;
    }
    public boolean isEmpty(){
        return rear==front;
    }

    public boolean isFull(){
        return rear==maxSize-1;
    }

    public int head(){
        if (isEmpty()){
            throw new  RuntimeException("队列为空");
        }
        return queue[front+1];
    }

    public int foot(){
        if (isEmpty()){
            throw new  RuntimeException("队列为空");
        }
        return queue[rear];
    }

    public void printQueue(){
        System.out.println(Arrays.toString(queue));
    }
}
